package com.example.administrator.nurse.present;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.common;
import com.example.administrator.nurse.model.CommonViewHolder;
import com.example.administrator.nurse.model.bean.Patient_log_bean;
import com.example.administrator.nurse.util.recyclerViewHelper;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/5/3.
 */
public class PatientLogActivity extends BaseActivity {

    private recyclerViewHelper<Patient_log_bean> recyclerViewHelper;

    @Override
    protected int setLayotId() {
        return R.layout.patient_log_layout;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        getData(false);
    }

    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {
        stopLoadingDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {
        stopLoadingDialog();
        if(response!=null){
            switch (requestCode){
                case GetDataNormal:
                    recyclerViewHelper.getRecyclerView().refreshComplete();
                    dealWithJson(false, response);
                    break;
                case GetDataMore:
                    recyclerViewHelper.getRecyclerView().loadMoreComplete();
                    dealWithJson(true, response);
                    break;
            }
        }else{
            t("获取数据失败！");
        }
    }

    private final int GetDataNormal = 0x11;
    private final int GetDataMore   = 0x12;
    private int lid = 0;
    private void getData(boolean isLoadMore){
        startLoadingDialog();
        RequestParams requestParams = new RequestParams();
        requestParams.put("id", common.nId);
        requestParams.put("idOfPatient", getIntent().getStringExtra("idOfPatient"));
        if(isLoadMore){
            requestParams.put("lid", lid);
            doPost(GetDataMore,null,common.Host + "/Controller/LghPatrolController.php?func=GetLogData",requestParams);
        }else {
            doPost(GetDataNormal,null,common.Host + "/Controller/LghPatrolController.php?func=GetLogData",requestParams);
        }
    }

    private void dealWithJson(boolean isLoadMore,String json){
        try{
            JSONArray jsonArray = new JSONArray(json);
            int length = jsonArray.length();
            if(length>0 && !isLoadMore){
                recyclerViewHelper.list.clear();
            }
            for(int i=0;i<length;i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                Patient_log_bean bean = new Patient_log_bean();
                bean.setDateTime(getJSONValue(jsonObject, "patrolTime"));
                bean.setNurse(common.nName);
                bean.setState(getJSONINTValue(jsonObject, "status"));
                bean.setRecode(getJSONValue(jsonObject, "content"));

                if(i==length-1){
                    lid = getJSONINTValue(jsonObject, "id");
                }
                recyclerViewHelper.list.add(bean);
            }
        }catch (Exception e){
            Log.d("zzzzz", e.toString());
        }
    }

    private void initView(){

        configCommonHeader(PatientLogActivity.this,getIntent().getStringExtra("name")+"的巡视日志",1);
        recyclerViewHelper=new recyclerViewHelper<Patient_log_bean>(PatientLogActivity.this,(XRecyclerView) findViewById(R.id.recyclerView))
                .withAdapter(R.layout.patient_bean_log)
                .withPullRefrshOpen(true)
                .withBindView(new recyclerViewHelper.BindViewListener() {

                    @Override
                    public void bindView(CommonViewHolder holder,final int position) {
                        TextView dateTime,state,nurse,recode;
                        dateTime=holder.getTextView(R.id.datetime);
                        state=holder.getTextView(R.id.state);
                        nurse=holder.getTextView(R.id.nurse);
                        recode=holder.getTextView(R.id.recode);
                        dateTime.setText(recyclerViewHelper.list.get(position).getDateTime());
                        /**
                         * case 0:
                         onSelectStatus = 0;state.setText("在外出");break;
                         case 1:
                         onSelectStatus = 1;
                         state.setText("在病房");
                         break;
                         case 2:
                         onSelectStatus = 2;
                         state.setText("家属陪伴外出");
                         break;
                         case 3:
                         onSelectStatus = 3;
                         state.setText("外出检查");
                         break;
                         case 4:
                         onSelectStatus = 4;
                         state.setText("未记录");
                         * */
                        switch (recyclerViewHelper.list.get(position).getState()){
                            case 0:
                                state.setText("在外出");
                                break;
                            case 1:
                                state.setText("在病房");
                                break;
                            case 2:
                                state.setText("家属陪伴外出");
                                break;
                            case 3:
                                state.setText("外出检查");
                                break;
                            case 4:
                                state.setText("未记录");
                                break;
                        }

                        nurse.setText(recyclerViewHelper.list.get(position).getNurse());
                        recode.setText(recyclerViewHelper.list.get(position).getRecode());
                    }
                })
                .withRefresh(new recyclerViewHelper.refreshListener() {
                    @Override
                    public void refresh() {
                        getData(false);
                    }
                })
                .withLoadMore(new recyclerViewHelper.loadMoreListener() {
                    @Override
                    public void loadMore() {
                        getData(true);
                    }
                })
                .initRecyclerView();

        //recyclerViewHelper.addItemDecoration(DivierItemDecoration.VERTICAL_LIST);
    }


}
